<template>
    <div class="user-list">
        <el-card shadow="hover">
            <div slot="header" class="flex jusc">
                <div class="head-title">
                    <span>评论列表</span>
                    <span class="table-choose" v-show="multipleSelection.length > 0">已选中{{ multipleSelection.length
                    }}个</span>
                </div>
                <div class="flex">
                    <div class="head-input">
                        <el-input v-model="searchContent" prefix-icon="el-icon-search" @keyup.enter.native="search"
                            @blur="search"></el-input>
                    </div>
                    <div class="head-btn">
                        <!-- <el-button type="primary">新增</el-button> -->
                        <el-button type="danger" @click="deleteCommentByIds" size="medium">批量删除</el-button>
                    </div>
                </div>

            </div>
            <div>
                <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%"
                    v-loading="isLoading" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column label="用户名" width="150">
                        <template slot-scope="scope">{{ scope.row.username }}</template>
                    </el-table-column>
                    <el-table-column prop="fileName" label="文件名" width="160" align="center">
                        <template slot-scope="scope">{{ scope.row.fileName }}</template>
                    </el-table-column>
                    <el-table-column prop="content" label="评论内容" width="300" align="center">
                        <template slot-scope="scope">{{ scope.row.content }}</template>
                    </el-table-column>
                    <el-table-column prop="updateTime" label="更新时间" width="240" align="center">
                        <template slot-scope="scope">{{ scope.row.updateTime }}</template>
                    </el-table-column>
                    <el-table-column prop="status" label="可见状态" width="150" align="center">
                        <template slot-scope="scope">{{ scope.row.status==1?"可见":"不可见" }}</template>
                    </el-table-column>
                    <el-table-column prop="address" label="操作" align="center">
                        <template slot-scope="scope">
                            <el-button size="mini" :type="scope.row.status==1?'primary':'success'" @click="changeStatus(scope.row)">{{scope.row.status==1?"隐藏":"展示"  }}</el-button>
                            <el-button size="mini" type="danger" @click="deleteCommentById(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pag">
                    <el-pagination background layout="prev, pager, next" :current-page="currPage"
                        @current-change="handleCurrentChange" :page-size="pageSize" :total=total>
                    </el-pagination>
                </div>
        </el-card>
    </div>
</template>

<script>
export default {
    name: 'commentVue',
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            searchContent: "", // 搜索内容
            // 加载样式
            isLoading: false,
            // 分页
            currPage: 1,
            pageSize: 10,
            total: 1,
            updateData:{},
            // 多元表格删除id
            ids:[],
        }
    },
    mounted() {
        this.search()
    },
    methods: {
        
        // 获取评论
        async search() {
            this.isLoading = true
            let res = await this.$store.dispatch("getCommentList", {
                pageSize: this.pageSize,
                searchContent: this.searchContent,
                currPage: this.currPage,
            });
            this.isLoading = false
            if (res.success) {
                // console.log(res.data)
                this.tableData = res.data.list
                this.total = res.data.totalCount
                console.log(this.tableData)
            }
            else this.$global.resError(res)
        },
        // 改变状态
        changeStatus(val){
            this.updateData =  JSON.parse(JSON.stringify(val));   //深拷贝(浅拷贝用的引用会直接影响原来数据)
            if(this.updateData.status==1){
                this.updateData.status=2
            }else {
                this.updateData.status=1
            }
            this.updateComment(this.updateData)
            setTimeout(()=>{
                this.search()
            },100)

        },
        // 修改评论状态
        async updateComment(data){
            let res = await this.$store.dispatch("updateComment",data)
            if(res.success){
                this.$message.success("修改成功")
            }else{
                this.$message.error("修改失败")
            }
        },

        // 单删
        deleteCommentById(val){
            this.multipleSelection.push(val)
            this.deleteCommentByIds()
        },
        // 删除评论
        deleteCommentByIds(){
            if(this.multipleSelection.length<1){
                this.$message.error("请选择要删除的评论")
            }else {
                this.ids=[]
                this.multipleSelection.forEach((item)=>{
                    this.ids.push(item.id)
                })
                this.deleteComment()
                this.multipleSelection=[]
                this.ids=[]
                setTimeout(()=>{
                    this.search()
                },100)
            }
        },
        // 删除评论
        async deleteComment(){
            let res = await this.$store.dispatch("deleteComment",this.ids)
            if(res.success){
                this.$message.success("删除成功")
            }else {
                this.$message.error(res.message)
            }
        },
        // 多选表格
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
         // 分页跳转
         handleCurrentChange(val) {
            console.log(`当前页为：${val}`)
            this.currPage = val
            this.search()
        },
        
    },
}
</script>

<style scoped>
.clearfix {
    display: flex;
    align-items: center;
}

.head-btn {
    margin: 0 10px;
}

.user-list {
    background: rgb(248, 248, 248);
}

.pag {
    text-align: center;
    margin-top: 20px;
}

.head-title {
    font-size: 17px;
    font-weight: 550;
}

.table-choose {
    font-size: 13px;
    color: gray;
    margin-left: 10px;
}

.flex {
    display: flex;
    align-items: center;
}

.jusc {
    justify-content: space-between;
}

.table-choose {
    font-size: 13px;
    color: gray;
    margin-left: 10px;
}
</style>